﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Example16.aspx.cs" Inherits="Chapter27.Example16" %>

<!DOCTYPE html>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>代码清单 27-16 使用计时器</title>
    <meta name="author" content="Adam Freeman" />
    <meta name="description" content="A simple example" />
</head>
<body>
    <h1 id="msg"></h1>
    <p>
        <button id="settime">Set time</button>
        <button id="cleartime">Clear Time</button>
        <button id="setinterval">Set Interval</button>
        <button id="clearinterval">Clear Interval</button>
    </p>
    <script>
        var buttons = document.getElementsByTagName("button");
        for (var i = 0; i < buttons.length; i++) {
            buttons[i].onclick = handleButtonPress;
        }

        var timeId;
        var intervalId;
        var count = 0;

        function handleButtonPress(e) {
            if (e.target.id == "settime") {
                timeId = window.setTimeout(function () {
                    displayMsg("Timeout Expired");
                }, 5000);
                displayMsg("Timeout Set");
            } else if (e.target.id == "cleartime") {
                window.clearTimeout(timeId);
                displayMsg("Timeout Cleared");
            } else if (e.target.id == "setinterval") {
                intervalId = window.setInterval(function () {
                    displayMsg("Interval expired. Counter: " + count++);
                }, 2000);
            } else if (e.target.id == "clearinterval") {
                window.clearInterval(intervalId);
                displayMsg("Interval Cleared");
            }
        }

        function displayMsg(msg) {

            document.getElementById("msg").innerHTML = msg;
        }
    </script>
</body>
</html>
